using IQIGame.Onigao.GamePlay;

namespace IQIGame.Onigao.Logic
{
    /// <summary>
    /// 移动进指定范围触发
    /// </summary>
    public class TrigMoveInRange : BaseServiceTrigger
    {
        private int range;
        /// <summary>
        /// 是否是进入范围触发(0：是，1：不是，2：都可以触发)
        /// </summary>
        private ServiceTriggerMoveRangeType rangeTriggerType;

        public override void Init(BaseServiceTriggerConfig paramConfig)
        {
            base.Init(paramConfig);
            var config = _paramConfig as TrigParamMoveInRange;
            if (config == null || config.range <= 0)
            {
                LogicLog.LogError($"触发器[{nameof(TrigMoveInRange)}]参数错误。range: {config.range}");
                return;
            }
            range = config.range;
            rangeTriggerType = (ServiceTriggerMoveRangeType)config.rangeTriggerType;
        }

        public override void SetData(LevelService service, BaseLevelFunctionHost host)
        {
            base.SetData(service, host);
            if (range > LgLevelConst.HeroQuadQueryHalfSize)
            {
                LogicLog.LogError($"初始化触发器(Type {ServiceTriggerType.MoveInRange})错误：参数0（距离）不能大于{LgLevelConst.HeroQuadQueryHalfSize}，否则将不能正确触发。实体mcid：{(host as LevelEntity)?.entityConfig?.id}，服务：{_service.config.id}。");
            }
        }

        public override bool OnTrigger(LevelPlayer player, object dynamicParam, TrigValueTypeParam dynamicValueTypePram)
        {
            if (range > LgLevelConst.HeroQuadQueryHalfSize)
            {
                return false;
            }
            LevelEntity entity = _host as LevelEntity;
            if (entity == null)
            {
                LogicLog.LogError("范围移动触发器entity为空，该触发器只对实体生效。");
                return false;
            }
            bool needTriggerIn = false;
            bool needTriggerOut = false;
            float curSqrDis = (entity.position - player.entity.position).sqrMagnitude;
            float prevSqrDis = player.entity.GetPrevEntityDisFromMe(entity.id);
            int sqrRange = range * range;
            if ((prevSqrDis == -1 || prevSqrDis > sqrRange) && curSqrDis <= sqrRange) //上一次移动不在范围内，现在在了
            {
                needTriggerIn = true;
                LogicLog.Log($"进入实体范围 实体mcid：{entity.entityConfig?.id}, range {range}cm.");
            }
            else if ((prevSqrDis != -1 && prevSqrDis <= sqrRange) && curSqrDis > sqrRange) //上一次移动在范围内，现在出范围了
            {
                needTriggerOut = true;
                LogicLog.Log($"离开实体范围 实体mcid：{entity.entityConfig?.id}, range {range}cm.");
            }

            if (((needTriggerIn || needTriggerOut) && rangeTriggerType == ServiceTriggerMoveRangeType.AnyWay)
                || (needTriggerIn && rangeTriggerType == ServiceTriggerMoveRangeType.Approach)
                || (needTriggerOut && rangeTriggerType == ServiceTriggerMoveRangeType.FarAway))
            {
                return base.OnTrigger(player, dynamicParam, dynamicValueTypePram);
            }
            return false;
        }

        public override void OnReset()
        {
            base.OnReset();
            range = 0;
            rangeTriggerType = 0;
        }
    }
}
